#a guessing game where a number 1 through 10 is generated and the user gets 3 tries to guess

#Fine imports
import random
import os

#
#
#
############################
#debug show number toggle
debug = 0
##########################################
### Stores Messages
class Message:
        #Message storage
        block = """
______                _                   _   _                 _               
| ___ \              | |                 | \ | |               | |              
| |_/ /__ _ _ __   __| | ___  _ __ ___   |  \| |_   _ _ __ ___ | |__   ___ _ __ 
|    // _` | '_ \ / _` |/ _ \| '_ ` _ \  | . ` | | | | '_ ` _ \| '_ \ / _ \ '__|
| |\ \ (_| | | | | (_| | (_) | | | | | | | |\  | |_| | | | | | | |_) |  __/ |   
\_| \_\__,_|_| |_|\__,_|\___/|_| |_| |_| \_| \_/\__,_|_| |_| |_|_.__/ \___|_|   

        
 _____                                                                          
|  __ \                                                                         
| |  \/ __ _ _ __ ___   ___                                                     
| | __ / _` | '_ ` _ \ / _ \                                                    
| |_\ \ (_| | | | | | |  __/                                                    
 \____/\__,_|_| |_| |_|\___|   """              
        intro = "WELCOME! I have right here a number from 1 to 10.\nYou have three tries to guess it!"
        FirstGuess = "Tell me what you think the number is?"
        retry = 'Hmm... \n*checks notes* \nThat number is wrong. \nTry again!', 'You go \nSorry, I read that wrong. \nYou didn\'t get it right. \nTry again.', 'Aww. Wrong number.\nTry again!'
        lasttry = 'One more time!\nYou can do it!', 'IT IS THE FINAL ROUND OF THE MATCH!\nFIGHT!', 'This is your last chance!\nWhat is your guess?'
        victorymsg = 'You did it!', 'Congratulations!', 'You won!', 'Victory!'
        gameover = 'You tried your best! \nBut.... this is a guessing game so you really couldn\'t do much', 'Sorry, all those guesses are wrong :(', 'All were wrong, try again later!'
############ All three print messages
        def open(self):
                print(self.block)
                print(self.intro)
### These next Two print random messages for the situation
        def win(self):
                
                print(random.choice(self.victorymsg))
        def gover(self):
                
                print(random.choice(self.gameover))
class Menu:
        state = 0
        command ={'1':1,'Play':1,'play':1,'quit':666,'Quit':666,'QUIT':666,'score':3,'Score':3,'Q':666,'2':3,'q':666,'8':8,'9':9}
        
        
        intmenu = "Do you want to Play? (1/Play) Quit (Q/Quit) or view the High Score? (2/Score)"
        def display(self):
                # Displays the menu choices
                
                
                print(self.intmenu)
                
        def ginput(self):
                #gets user choice
                letter = input("Choice: ")
                while letter not in self.command:
                        letter = input("Choice: ")
                self.state = self.command[letter]
        def returntomenu(self):
                #Brings us back to the menu
              
                self.state = 0
                return self.state


class Scoring:
        #Scoring is winstreaks. Stores one number
        wins = 0
        def __init__(self):
                #opens save file if it exists, makes it if it doesn't
                if os.path.isfile('./score0.sav'):
                        print('OPENING OLD SAV')
                        sav = open('score0.sav','r')
                        infi = sav.read()
                        if infi.isdigit():
                                self.wins = int(float(infi))
                        sav.close()
                else:
                        print('WRITING NEW FILE')
                        sav = open('score0.sav','w')
                        sav.write(str(self.wins))
                        sav.close()
        def addscore(self):
                #adds to the score and writes it
                self.wins += 1
                print('WRITING TO FILE')
                sav = open('score0.sav','w')
                sav.write(str(self.wins))
                sav.close()
                return self.wins
        def viewscore(self):
                #prints the current score
                print("Current Win Streak",self.wins)
        def losereset(self):
                #resets the score on loss and writes it
                print("The score is reset!")
                self.wins = 0
                print('WRITING NEW FILE')
                sav = open('score0.sav','w')
                sav.write(str(self.wins))
                sav.close()
class Guessing:
        #Heart of the guessing game
        maxtries = 3
        tries = 0
        answer = 0
        

        rando = 0
        
        def __init__(self):
                #generates random number 1 to 10
                self.rando = random.randint(1,10)
        


        def getanswer(self):
                #gets an answer from user and checks if it is valid (within range and an int)
                self.answer=99 #hack
                while not (self.answer <=10 and self.answer >=0):
                        answer = input("My answer is: ")
                        if answer.isdigit():
                                self.answer= int(answer)

              
        def checkanswer(self):
                #checks if the answer is the random number
                if self.answer == self.rando:
                        return True
                else:
                        return False

        def checktries(self):
               # checks if you answered too many times
                if self.tries == self.maxtries:
                        return True
                if self.tries < self.maxtries:
                        return False
                
        def addtry(self):
                #adds a try each time you try.. and FAIL
                self.tries += 1
                

        def checkend(self):
                #### checks if it should end the loop or not and go to an end. debug print below
                
                #print(self.answer,self.rando,self.tries,self.maxtries)
                if self.answer == self.rando or self.tries >= self.maxtries:
                        return True
                        
                else:
                        return False

#######   main code !!!!!!!!!!!!!!!!!!!##############################
#declares objects n' stuff
hiscore = Scoring()
intro = Message()
mmenu = Menu()
intmenu = mmenu.intmenu
wins = 0

intro.open()
while 1:
        while mmenu.state == 0:
        
                #main menu loop
                if debug == 1:
                        print(mmenu.state)
                mmenu.display()
                mmenu.ginput()
                if debug == 1:
                        print(mmenu.state)
                if mmenu.state == 3:
                        #highscore
                
                        
                        hiscore.viewscore()
                        mmenu.returntomenu()
        #### Debug toggle
                if mmenu.state == 9:
                        print("debug on")
                        debug = 1
                        mmenu.returntomenu()
                if mmenu.state == 8:
                        print("debug off")
                        debug = 0
                        mmenu.returntomenu()
        
        if mmenu.state == 1:
#################### main game loop zone
                print(intro.FirstGuess)
                newgame = Guessing()
                if debug == 1:
                        print(newgame.rando)
                
                newgame.getanswer()

                newgame.addtry()
                ############# asks more here
                while not  newgame.checkend():

                        if newgame.tries == 1:
                                print(random.choice(intro.retry))
                        if newgame.tries == 2:
                                print( random.choice(intro.lasttry))
                        newgame.getanswer()
	                
                        newgame.addtry()
                #win/lose if/else stuff
                if newgame.checkanswer():
                        
                        hiscore.addscore()
                        intro.win()
                        mmenu.returntomenu()
                elif newgame.checktries():

                        intro.gover()
                        print("The number was:", newgame.rando)

                        hiscore.losereset()
                        mmenu.returntomenu()
                else:
                        print("this shouldn't happen")
        
                del newgame #deltes game object

        if mmenu.state == 666:
                ##quitter
                print("Bye!")
                quit()

#


